var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
var app = getApp()
Page({

  /**
   * 页面的初始数据key_words=yuanchengkaifang&j_id=27&ser_key=yuanchengkaifang&pinyin=yuanchengkaifang&inquiry=4&conets={"text":"少时诵诗书所所所所所所所所所所所","text2":"","timleng":"","upload_picture_list":[],"typedate":0}&text=undefined
   */
  data: {
    inquiry: "",
    cartpop: false, //购物车
    detailspop: false, //详情
    sum: 0, //总数
    kinds: 0, //种类
    cartlist: [], //购物车数组
    steps: ["医生开方", "选择药品", "医生医嘱", '发送处方'],
    stepnum: 1,
    druplist: [],
    back_orser: 0,
    zid: 0,
  },
  //下一步
  handleNextStep(e) {

    var that = this
    var cartlist = that.data.cartlist
    var ifzj = that.data.ifzj
    var zid = that.data.zid
    var back_orser = that.data.back_orser
    var goodstype = 1
    if (that.data.conets) {
      var text = that.data.conets.text
      var conets = JSON.stringify(that.data.conets)
    }
    if (that.data.text) {
      var text = that.data.text
    }

    var key_words = that.data.key_words
    if (ifzj == '1') {
      var role = '1'
    } else {
      var role = '0'
    }
    var arr1 = []
    for (var i = 0; i < cartlist.length; i++) {
      arr1.push({
        sname: cartlist[i].sname,
        smoney: cartlist[i].smoney,
        snum: cartlist[i].snum,
        sthumb: cartlist[i].sthumb,
        use: cartlist[i].use,
        num: cartlist[i].num,
        sid: cartlist[i].sid,
        s_id: cartlist[i].s_id,
        yf_id: cartlist[i].yf_id,
      })
    }
    console.log(arr1)
    // var nwelist = JSON.stringify(arr1)
    var nwelist = wx.setStorageSync('cartlist', arr1)
    console.log(nwelist)
    var c_id = that.data.c_id
    var text = that.data.text
    var zhenduan = that.data.zhenduan
    var yongyao = that.data.yongyao
    var j_id = that.data.j_id
    var inquiry = that.data.inquiry
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/yizhu/yizhu?c_id=' + c_id + '&text=' + text + '&zhenduan=' + zhenduan + '&j_id=' + j_id + '&yongyao=' + yongyao + '&role=' + role + '&inquiry=' + inquiry + '&conets=' + conets + '&key_words=' + key_words + '&back_orser=' + back_orser + '&zid=' + zid + '&goodstype=1',
    })
  },
  //购物车
  handleShowSelectDrugs() {
    this.setData({
      cartpop: true
    })
  },
  handleClosecart() {
    this.setData({
      cartpop: false
    })
  },
  //详情
  drupdetail(e) {
    var that = this
    var index = e.currentTarget.dataset.dex
    var druplist = that.data.druplist
    var detail = druplist[index]
    this.setData({
      detailspop: true,
      detail: detail
    })
  },
  //关闭详情
  handleCloseTap() {
    this.setData({
      detailspop: false
    })
  },
  //加 减
  handleNumberClick(e) {
    console.log("e", e)

    let types = e.currentTarget.dataset.type,
      i = e.currentTarget.dataset.dex,
      arrs = this.data.druplist,
      id = e.currentTarget.dataset.id,
      cratlist = this.data.cartlist,
      self = this
    if (types == 'add') {
      ++arrs[i].num
    } else if (types == 'reduce') {
      if (arrs[i].num > 1) {
        --arrs[i].num
      } else {
        arrs[i].num = 0
      }
    } else if (types == 'adds') {
      var index = e.currentTarget.dataset.index
        ++cratlist[index].num
      for (var n = 0; n < arrs.length; n++) {
        if (arrs[n].s_id == id) {
          arrs[n].num = cratlist[index].num
        }
      }

    } else if (types == 'reduces') {
      var index = e.currentTarget.dataset.index
      if (cratlist[index].num > 1) {
        --cratlist[index].num
      } else {
        cratlist[index].num = 0
        cratlist.splice(index, 1)
      }
      for (var m = 0; m < arrs.length; m++) {
        if (arrs[m].s_id == id) {
          arrs[m].num = cratlist[index].num
        }
      }
    }

    if (cratlist.length == 0) {
      self.setData({
        cartpop: false
      })
    }
    this.setData({
      druplist: arrs,
      cartlist: cratlist
    })
    this.sunmfun()
    this.typefun()
  },
  //删除购物车
  deletedrup(e) {
    let i = e.currentTarget.dataset.dex,
      cratlist = this.data.cartlist,
      arrs = this.data.druplist,
      id = e.currentTarget.dataset.id
    arrs.map(function (list) {
      if (id == list.id) {
        list.num = 0
      }
    })
    cratlist.splice(i, 1)
    if (cratlist.length == 0) {
      this.setData({
        cartpop: false
      })
    }
    this.setData({
      cartlist: cratlist,
      druplist: arrs
    })
    this.sunmfun()
    this.typefun()
  },
  //获取总数
  sunmfun() {
    let arrs = this.data.druplist,
      sum = 0
    arrs.map(function (list) {
      sum += list.num
    })
    this.setData({
      sum
    })
  },
  //获取种类
  typefun() {
    let arrs = this.data.druplist,
      tsum = 0
    arrs.map(function (list) {
      if (list.num != 0) {
        ++tsum
      }
    })
    this.setData({
      kinds: tsum
    })
  },
  //选择该药
  AddClick(e) {
    let i = e.currentTarget.dataset.dex,
      arrs = this.data.druplist,
      cartlist = this.data.cartlist
    arrs[i].num = 1
    cartlist.push(arrs[i])
    this.setData({
      druplist: arrs,
      cartlist
    })
    this.sunmfun()
    this.typefun()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var zhenduan = options.zhenduan
    var yongyao = options.yongyao
    var text = options.text
    var bgc = wx.getStorageSync('color')
    var c_id = options.c_id
    var j_id = options.j_id
    var ifzj = options.ifzj
    var inquiry = options.inquiry
    var key_words = options.key_words
    var hid = options.hid
    var zid = options.zid
    if (options.back_orser) {
      var back_orser = options.back_orser
    } else {
      var back_orser = 0
    }

    //1、获取当前位置坐标

    if (options.conets !== 'undefined') {
      var conets = JSON.parse(options.conets)
      that.setData({
        conets: conets
      })
    }

    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    this.setData({
      zhenduan: zhenduan,
      yongyao: yongyao,
      text: text,
      c_id: c_id,
      j_id: j_id,
      ifzj: ifzj,
      inquiry: inquiry,
      key_words: key_words,
      hid: hid,
      back_orser: back_orser,
      zid: zid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getallgoods()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.sunmfun()
    this.typefun()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getallgoods() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/goods.allgoods',
      data: {
        hid: that.data.hid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          druplist: res.data
        })
      }
    })
  }
})